Repository navigation
fix(typescript): drain queued events before ending the stream iterator - #44
Open
OsamaAnsar wants to merge 1 commit into
Open
OsamaAnsar wants to merge 1 commit into
OsamaAnsar wants to merge 1 commit into
Conversation
The async iterator on EventDrivenStream exited its loop as soon as the close or error flag was set, discarding any events that were already in its queue. A server that sends a few posts and then ends the response (or a connection that fails right after data arrived) made `for await (const event of stream)` yield nothing. Keep looping while the queue is non-empty, and only finish (or throw the recorded error) once it has been drained. Adds a jest test template covering the close and error cases.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
EventDrivenStream's async iterator (xdk-gen/templates/typescript/event_driven_stream.j2) loops onwhile (!isComplete && !hasError). As soon as thecloseorerrorevent sets a flag, the loop exits, so anything already sitting indataQueueis thrown away.Repro: the server sends three posts in one chunk and ends the response.
With the stream left open (control), the same iterator yields all three (
["1","2","3"]). The same loss happens when the connection errors right after data arrives: the queued events are dropped and the error is thrown immediately.Fix
Keep looping while the queue is non-empty and only stop once it is drained:
The recorded error (if any) is still thrown, but only after the queue is empty. Behavior for a stream that is still open is unchanged.
Tests
New test template
test_event_driven_stream.j2(registered ingenerator.rsnext totest_client_errors) writestests/event_driven_stream.test.ts, covering:How this was verified
I do not have a Rust toolchain, so I did not run the generator or
cargo test/cargo fmt/cargo clippy; CI will regenerate the SDK. Instead I applied the identical one-line change by hand to a copy of the currentxdk-typescriptoutput (v0.6.6) and ran the new test file there withnpm ci+ jest:[])tsc --noEmitcleanThe only Rust change is one added
renderline in thetests:list.